#===============================================================================
#  File:    0A1-AppendixB2-Negations.R
#  Date:    June, 2022 
#  Author:  Natalia Umansky
#  Purpose: Replicating figures displayed in the Appendix B.2
#===============================================================================

# LIBRARIES
#===============================================================================
require(quanteda)
require(LSX)
library(data.table)
library(rtweet)
library(stringr)
library(stringi)
library(qdapRegex)
library(tidyverse)
library(kableExtra)
library(ggplot2)
library(cowplot)

quanteda_options(threads = 8)


# IMPORT DATA
#===============================================================================

Groups <- c("Politicians", "Media", "Citizens", "Advocates", "Friends")

for (i in Groups){
  df <- fread(paste0("~/",i,".csv"), 
              select = c("user_id", "status_id", "created_at", "screen_name", "text", "hashtags"))
  assign(i, df)
}

ALL <- rbind(Media, Politicians, Citizens, Advocates, Friends)


# FUNCTIONS
#===============================================================================

# Cleaning text

cleaning <- function(x){
  corp <- corpus(x)
  
  tokens<- corp %>% 
    #corpus_reshape("sentences") %>% 
    tokens(remove_punct = TRUE) %>% 
    tokens_select("^[0-9a-zA-Z]+$", valuetype = "regex")  %>% 
    tokens_tolower() %>% 
    tokens_compound(pattern = list(c("no", "safe"), c("not", "safe"),
                                   c("no", "normal"), c("not", "normal"),
                                   c("no", "peace"), c("not", "peace"),
                                   c("new", "zealand"), c("united", "states"),
                                   c("united", "kingdom"), c("north", "korea"),
                                   c("sri", "lanka"), c("el", "paso"),
                                   c("no", "danger"), c("no", "harm"),
                                   c("not", "an", "emergency"),
                                   c("strong", "ties"),
                                   c("existential", "threat"),
                                   c("existential", "threats"),
                                   c("not", "a", "threat"),
                                   c("existential", "crisis"),
                                   c("deeply", "concearned"),
                                   c("take", "action"),
                                   c("under", "attack"),
                                   c("out", "of", "danger"))) 
  
  
  
  return(tokens)
}

# Peace
#===============================================================================

peace <- ALL %>%
  filter(str_detect(text, regex('\\bpeace\\b')))

toks <- cleaning(peace)

fcmat <- fcm(toks, context = "document", tri = FALSE)

feat <- names(topfeatures(fcmat, 50))

net_peace <- fcm_select(fcmat, pattern = feat) %>%
  textplot_network(min_freq = 0.5, omit_isolated=T)

ggsave("Figure-A2-peace.pdf", net_peace)

# Order
#===============================================================================

order <- ALL %>%
  filter(str_detect(text, regex('\\border\\b')))

toks <- cleaning(order)

fcmat <- fcm(toks, context = "document", tri = FALSE)

feat <- names(topfeatures(fcmat, 50))

net_order <- fcm_select(fcmat, pattern = feat) %>%
  textplot_network(min_freq = 0.5, omit_isolated=T)

ggsave("Figure-A2-order.pdf", net_order)

# Peaceful
#===============================================================================

peaceful <- ALL %>%
  filter(str_detect(text, regex('\\bpeaceful\\b')))

toks <- cleaning(peaceful)

fcmat <- fcm(toks, context = "document", tri = FALSE)

feat <- names(topfeatures(fcmat, 50))

net_peaceful <- fcm_select(fcmat, pattern = feat) %>%
  textplot_network(min_freq = 0.5, omit_isolated=T)

ggsave("Figure-A2-peaceful.pdf", net_peaceful)

# Everyday
#===============================================================================

everyday <- ALL %>%
  filter(str_detect(text, regex('\\beveryday\\b')))

toks <- cleaning(everyday)

fcmat <- fcm(toks, context = "document", tri = FALSE)

feat <- names(topfeatures(fcmat, 50))

net_everyday <- fcm_select(fcmat, pattern = feat) %>%
  textplot_network(min_freq = 0.5, omit_isolated=T)

ggsave("Figure-A2-everyday.pdf", net_everyday)

# Unity
#===============================================================================

unity <- ALL %>%
  filter(str_detect(text, regex('\\bunity\\b')))

toks <- cleaning(unity)

fcmat <- fcm(toks, context = "document", tri = FALSE)

feat <- names(topfeatures(fcmat, 50))

net_unity <- fcm_select(fcmat, pattern = feat) %>%
  textplot_network(min_freq = 0.5, omit_isolated=T)

ggsave("Figure-A2-unity.pdf", net_unity)

# Treaty
#===============================================================================

treaty <- ALL %>%
  filter(str_detect(text, regex('\\btreaty\\b')))

toks <- cleaning(treaty)

fcmat <- fcm(toks, context = "document", tri = FALSE)

feat <- names(topfeatures(fcmat, 50))

net_treaty <- fcm_select(fcmat, pattern = feat) %>%
  textplot_network(min_freq = 0.5, omit_isolated=T)

ggsave("Figure-A2-treaty.pdf", net_treaty)



